﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL;
using SalesSys.DAL.Models;
using SalesSys.DAL;

namespace SalesSys.BLL
{
    public class CustomerBLL : BaseBLL<Customer, Guid>
    {
        public KeyValuePair<int, IEnumerable<Customer>> Select(string searchKey, DateTime? startTime,DateTime? endTime,ApprovalState? state, QueryInfo qinfo,bool? isDel=false)
        {

            var query = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(searchKey))
            {
                query = query.Where(p => p.Name.Contains(searchKey) || p.ShortName.Contains(searchKey) || p.ContactPhone.Contains(searchKey) || p.Contacts.Contains(searchKey)||p.ClientManager.Name.Contains(searchKey));
            }
            if (startTime!=null)
            {
                query = query.Where(p=>p.CreateTime>=startTime);
            }
            if (endTime != null)
            {
                endTime = endTime.Value.AddDays(1);
                query = query.Where(p => p.CreateTime <= endTime);
            }
            if (state != null)
                query = query.Where(p => p.ApprovalState == state);
            if (isDel!=null)
            {
                query = query.Where(p => p.IsDelete == isDel);
            }
            var count = query.Count();
            var list = query.OrderByDescending(p => p.CreateTime).Paging(qinfo.Page, qinfo.Rows).ToList();
            return new KeyValuePair<int, IEnumerable<Customer>>(count, list);
        }

        public KeyValuePair<int, IEnumerable<Customer>> Select(Guid empId, QueryInfo qinfo)
        {

            var query = Rep.Entities;

            query = query.Where(p => p.ClientManager.Id == empId);

            var count = query.Count();
            var list = query.OrderByDescending(p => p.CreateTime).Paging(qinfo.Page, qinfo.Rows).ToList();
            return new KeyValuePair<int, IEnumerable<Customer>>(count, list);
        }

        /// <summary>
        /// 通过名称判断公司信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Customer GetCustomerByName(string name)
        {
            
            if (name == null)
                return null;
            Customer customer = Rep.Entities.FirstOrDefault(p => p.Name == name);
            return customer;
        }
        /// <summary>
        /// 根据客户ID以及客户经理Id查询对应的户头
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="empId"></param>
        /// <returns></returns>
        public Customer GetCustomerByIdAndEmpId(Guid customerId,Guid empId)
        {
            Customer customer = Rep.Entities.FirstOrDefault(p => p.Id == customerId&&p.ClientManager.Id==empId);
            return customer;
        }
        /// <summary>
        /// 通过名称判断公司信息
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public Customer ExistByName(Customer customer)
        {

            if (customer == null)
                return null;
            var q = Rep.Entities;

            Customer customer1 = Rep.Entities.FirstOrDefault(p => (p.Name == customer.Name || p.ShortName == customer.ShortName)&&!p.IsDelete&&p.ApprovalState!=ApprovalState.Disagree);
            return customer1;
        }
        /// <summary>
        /// 审核客户
        /// </summary>
        /// <returns></returns>
        public bool Apply(Customer customer)
        {
            int tf = this.Edit(customer);
            return tf > 0;
        }

        /// <summary>
        /// 添加现金户头
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int CreateCashAccount(Customer entity)
        {
            if (entity.Accounts == null)
                entity.Accounts = new List<Account>();
            entity.Accounts.Add(new Account() { Name = CashAccountName, IsCash = true });
            return Rep.Update(entity);
        }

        /// <summary>
        /// 如果当前不存在现金户，则添加现金户头到实体对象，不更新数据库,
        /// </summary>
        /// <param name="entity"></param>
        public void AddCashAccountIfNon(Customer entity)
        {
            if (entity.Accounts == null)
                entity.Accounts = new List<Account>();
             if (!entity.Accounts.Any(p => p.IsCash))
            {
                entity.Accounts.Add(new Account() { Name = CashAccountName, IsCash = true });
            }
        }
        public static  readonly string CashAccountName = "现金户头";
    }
}
